
package com.ukao.pad.presenter;


import com.ukao.pad.base.BasePresenter;
import com.ukao.pad.bean.BalanceBean;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.DecimalUtil;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.view.QuiltSweepView;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class QuiltSweepPesenter extends BasePresenter<QuiltSweepView> {

    public QuiltSweepPesenter(QuiltSweepView view) {
        attachView(view);
    }


    /**
     * 二维码 支付
     * @param orderId
     * @param balance
     * @param isWeChat  是否为微信
     */

    public void wxUnifiedOrder (String orderId,String  balance,boolean  isWeChat,boolean  isUseUPay,String  cpnRelId) {
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        if(isUseUPay){
            mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡
        }else{
            mHashMap.put("balance",balance);
        }
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        mvpView.showLoading();
        Observable mobservable;
        if (isWeChat){
            mobservable = apiStores.wxUnifiedOrder(Constant.createParameter(mHashMap));
        }else{
            mobservable= apiStores.aliUnifiedOrder(Constant.createParameter(mHashMap));
        }

        addSubscription(mobservable,
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadQRCodeData(model.getData());
                        }else{
                            mvpView.scanPayMicroFail(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     查询交易状态
     */
    public void queryStatusBySeq (String seq  ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("seq", seq);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.queryStatusBySeq(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            mvpView.queryStatusData(model.getData());
                        } else {
                            mvpView.loadFail(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                    }

                });


    }


    /**
     * 支付解锁
     * @param orderId
     */
    public void unlockOrder(String orderId    ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.unlockOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {

                            mvpView.unlockOrderData(  );

                        } else {

                            mvpView.unlockOrderFail();
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {

                    }

                });
    }

    /**
     * 微信刷卡支付-主扫
     * @param orderId
     * @param balance
     * @param paymentCode
     * @param   isWeChat  是否是微信
     */
    public void wxMicroPay (String orderId,String  balance,String  paymentCode,boolean  isWeChat,boolean  isUseUPay,String  cpnRelId) {
        mvpView.showLoading();
        HashMap     mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId  );
        mHashMap.put("authCode",paymentCode);
        if (!CheckUtils.isEmpty(cpnRelId)){
            mHashMap.put("cpnRelId", cpnRelId);
        }
        if(isUseUPay){
            mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡
        }else{
            mHashMap.put("balance",balance);
        }
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        Observable  mobservable=null;
        if (isWeChat){
            mobservable = apiStores.wxMicroPay(Constant.createParameter(mHashMap));

        }else{
            mobservable = apiStores.aliMicroPay(Constant.createParameter(mHashMap));
        }

        addSubscription(mobservable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadPayData(model.getMsg());
                        }else{
                            mvpView.loadFail(model.getMsg());
                        }

                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }




}
